﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GalaxyDefenderChaos
{
    public class My2DOptionMenu : My2DMenu
    {
        List<OptionMenuListener> _Listeners = new List<OptionMenuListener>();

        string[] _StrTextureButtons = {
                                    "Menu/Option/SoundOn",
                                    "Menu/Option/SoundOff",
                                    "Menu/Option/NormalLevel",
                                    "Menu/Option/HardLevel",
                                    "Menu/Back"
                                };
        Vector2[] _ButtonPositions = { 
                                        new Vector2(180, 220),
                                        new Vector2(450, 220),
                                        new Vector2(180, 320),
                                        new Vector2(450, 320),
                                        new Vector2(310, 420)
                                     };

        public My2DOptionMenu(ContentManager content, GraphicsDevice graphicsDevice)
        {
            initBaseMenu(content, graphicsDevice);
            _ButtonList = new My2DButton[_StrTextureButtons.Count()];
            for (int i = 0; i < _ButtonList.Count(); i++)
            {
                _ButtonList[i] = new My2DButton(content, _StrTextureButtons[i], _ButtonPositions[i], new Vector2(250, 45));
            }
        }

        public void AddListener(OptionMenuListener listener)
        {
            _Listeners.Add(listener);
        }

        public override void Update(GameTime gameTime, TouchCollection touchCollection)
        {
            if (touchCollection.Count > 0 && touchCollection[0].State == TouchLocationState.Pressed)
            {
                Vector2 touchPoint = new Vector2(touchCollection[0].Position.X, touchCollection[0].Position.Y);
                int index = GetMenuSelectedIndex(touchPoint);
                switch (index)
                {
                    case 0:
                        NotifySoundOnSelectedEvent();
                        break;
                    case 1:
                        NotifySoundOffSelectedEvent();
                        break;
                    case 2:
                        NotifyNormalLevelSelectedEvent();
                        break;
                    case 3:
                        NotifyHardLevelSelectedEvent();
                        break;
                    case 4:
                        NotifyBackToMainMenuSelectedEvent();
                        break;
                }
            }
        }

        private void NotifyBackToMainMenuSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].BackToMainMenuSelected();
            }
        }

        private void NotifyHardLevelSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].HardLevelSelected();
            }
        }

        private void NotifyNormalLevelSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].NormalLevelSelected();
            }
        }

        private void NotifySoundOffSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].SoundOffSelected();
            }
        }

        private void NotifySoundOnSelectedEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].SoundOnSelected();
            }
        }
    }
}
